package com.aier.classes.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.classes.common.InviteRequestUtil;
import com.aier.classes.entity.InviteParent;
import com.aier.common.common.SystemDateFormatUtil;


public class InviteParentRowMapper implements RowMapper<InviteParent> {

    private int type;

    public static final int AND_STUDENTNAME = 1;
    public static final int AND_INVITETEACHER = 2;
    public static final int AND_STUDENTNAME_MESSAGE = 3;
    
    public InviteParentRowMapper() {
    }

    public InviteParentRowMapper(int type) {
        this.type = type;
    }

    @Override
    public InviteParent mapRow(ResultSet rs, int rowNum) throws SQLException {
        InviteParent ip = parseInviteParent(new InviteParent(), rs);
        ip.setCtime_str(SystemDateFormatUtil.formatDate(ip.getCtime(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));
        ip.setState_str(InviteRequestUtil.InviteStateMap.get(ip.getState()));
        
        switch (type) {
            case AND_STUDENTNAME:
                ip.setName_student(rs.getString("name_student"));
                break;
            case AND_INVITETEACHER:
                ip.setName_teacher(rs.getString("name_teacher"));
                break;
            case AND_STUDENTNAME_MESSAGE:
                ip.setName_student(rs.getString("name_student"));
                ip.setName_teacher(rs.getString("name_teacher"));
                ip.setName_class(rs.getString("name_class"));
                ip.setName_school(rs.getString("name_school"));
                ip.setBaby_uid(rs.getLong("baby_uid"));
                break;    
        }
        return ip;
    }

    public static InviteParent parseInviteParent(InviteParent s, ResultSet rs) throws SQLException {
        return parseInviteParent(s, rs, null);
    }

    public static InviteParent parseInviteParent(InviteParent s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setIpid(rs.getLong(su+"ipid"));
        } catch (SQLException e) {
            s.setIpid(null);
        }
        try {
            s.setPname(rs.getString(su+"pname"));
        } catch (SQLException e) {
            s.setPname(null);
        }
        try {
            s.setPhone(rs.getString(su+"phone"));
        } catch (SQLException e) {
            s.setPhone(null);
        }
        try {
            s.setCsid(rs.getLong(su+"csid"));
        } catch (SQLException e) {
            s.setCsid(null);
        }
        try {
            s.setRelation(rs.getString(su+"relation"));
        } catch (SQLException e) {
            s.setRelation(null);
        }
        try {
            s.setTid(rs.getLong(su+"tid"));
        } catch (SQLException e) {
            s.setTid(null);
        }
        try {
            s.setCid(rs.getLong(su+"cid"));
        } catch (SQLException e) {
            s.setCid(null);
        }
        try {
            s.setState(rs.getShort(su+"state"));
        } catch (SQLException e) {
            s.setState(null);
        }
        try {
            s.setCtime(rs.getTimestamp(su+"ctime"));
        } catch (SQLException e) {
            s.setCtime(null);
        }
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, InviteParent s) {
        par.put("ipid",s.getIpid());
        par.put("pname",s.getPname());
        par.put("phone",s.getPhone());
        par.put("csid",s.getCsid());
        par.put("relation",s.getRelation());
        par.put("tid",s.getTid());
        par.put("cid",s.getCid());
        par.put("state",s.getState());
        par.put("ctime",s.getCtime());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, InviteParent s) {
        args.put("ipid",s.getIpid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final InviteParent s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "ipid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(InviteParent s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into invite_parent(");
        int count = 0;
        if (null != s.getPname()) {
            sql.append("pname,");
            count++;
        }

        if (null !=s.getPhone()) {
            sql.append("phone,");
            count++;
        }
        if (null != s.getCsid()) {
            sql.append("csid,");
            count++;
        }
        if (null != s.getRelation()) {
            sql.append("relation,");
            count++;
        }
        if (null != s.getTid()) {
            sql.append("tid,");
            count++;
        }
        if (null != s.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null != s.getState()) {
            sql.append("state,");
            count++;
        }
        if (null != s.getCtime()) {
            sql.append("ctime,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, InviteParent s)
        throws SQLException {
        int count = 1;
        if (null != s.getPname()) {
            ps.setString(count++,s.getPname());
        }
        if (null != s.getPhone()) {
            ps.setString(count++, s.getPhone());
        }
        if (null != s.getCsid()) {
            ps.setLong(count++,s.getCsid());
        }
        if (null != s.getRelation()) {
            ps.setString(count++,s.getRelation());
        }
        if (null != s.getTid()) {
            ps.setLong(count++,s.getTid());
        }
        if (null != s.getCid()) {
            ps.setLong(count++,s.getCid());
        }
        if (null != s.getState()) {
            ps.setShort(count++,s.getState());
        }
        if (null != s.getCtime()) {
            ps.setTimestamp(count++,s.getCtime());
        }


    }

}
